import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import net.sourceforge.jcalendarbutton.*;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import javax.swing.JEditorPane;
import java.awt.Color;
import javax.swing.JRadioButton;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import javax.swing.JOptionPane;
import javax.swing.ComboBoxModel;

public class CalendarApplication {

	private Custom customWindow;
	private JFrame frmIcsCalendar;
	private JTextField txtStartDate;
	private JTextField txtEndDate;
	private JTextField txtStartHour;
	private JTextField txtEndHour;
	private JTextField txtStartMin;
	private JTextField txtEndMin;
	private JTextField txtName;
	private JTextField txtLocation;
	private JTextField txtResources;
    private JTextField txtReminder;
	private javax.swing.ButtonGroup buttonGroup1;
	private JRadioButton chckbxNewCheckBox;
	private JRadioButton chckbxNewCheckBox_1;
	private JRadioButton chckbxNewCheckBox_2;
	private JRadioButton chckbxNewCheckBox_3;
	private JRadioButton chckbxNewCheckBox_4;
	private JButton btnResetFields;
	private JButton btnCreateEvent = new JButton("Create Event");
	private JRadioButton rdbtnNoRepeat;
	private PrintICS eventWriter = new PrintICS();
	private Date currentDate;
	private String customWindowString;
	private SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
	
	private boolean globalInvalidator = false;
	
	//New tzList for time zone combobox using TimeZoneEntry class.
	TimeZoneEntry[] tzList = {
			new TimeZoneEntry(TimeZone.getTimeZone("US/Samoa")),
			new TimeZoneEntry(TimeZone.getTimeZone("HST")),
			new TimeZoneEntry(TimeZone.getTimeZone("AST")),
			new TimeZoneEntry(TimeZone.getTimeZone("PST")),
			new TimeZoneEntry(TimeZone.getTimeZone("MST")),
			new TimeZoneEntry(TimeZone.getTimeZone("CST")),
			new TimeZoneEntry(TimeZone.getTimeZone("EST")),
			new TimeZoneEntry(TimeZone.getTimeZone("PRT")),
			new TimeZoneEntry(TimeZone.getTimeZone("AGT")),
			new TimeZoneEntry(TimeZone.getTimeZone("Atlantic/South_Georgia")),
			new TimeZoneEntry(TimeZone.getTimeZone("Atlantic/Cape_Verde")),
			new TimeZoneEntry(TimeZone.getTimeZone("GMT")),
			new TimeZoneEntry(TimeZone.getTimeZone("CET")),
			new TimeZoneEntry(TimeZone.getTimeZone("EET")),
			new TimeZoneEntry(TimeZone.getTimeZone("EAT")),
			new TimeZoneEntry(TimeZone.getTimeZone("NET")),
			new TimeZoneEntry(TimeZone.getTimeZone("PLT")),
			new TimeZoneEntry(TimeZone.getTimeZone("BST")),
			new TimeZoneEntry(TimeZone.getTimeZone("VST")),
			new TimeZoneEntry(TimeZone.getTimeZone("CTT")),
			new TimeZoneEntry(TimeZone.getTimeZone("JST")),
			new TimeZoneEntry(TimeZone.getTimeZone("AET")),
			new TimeZoneEntry(TimeZone.getTimeZone("SST")),
			new TimeZoneEntry(TimeZone.getTimeZone("NST")),
			new TimeZoneEntry(TimeZone.getTimeZone("MIT"))
	};
	
	//AM and PM string[] for combobox
	private String[] amPmList = {"AM", "PM"};
	private JTextField txtAttachment;
	private JLabel lblReminder;
	private JComboBox comboReminder;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CalendarApplication window = new CalendarApplication();
					window.frmIcsCalendar.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public CalendarApplication() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {		
		customWindowString="";
		frmIcsCalendar = new JFrame();
		frmIcsCalendar.setTitle("ICS 414 Calendar App - Team Ribier");
		frmIcsCalendar.setBounds(100, 100, 376, 548);
		frmIcsCalendar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmIcsCalendar.getContentPane().setLayout(null);
		frmIcsCalendar.setLocationRelativeTo(null);// set the location of this window at the middle of the screen
		
		JCalendarButton calendarStart = new JCalendarButton();
		calendarStart.setBounds(152, 4, 29, 29);
		frmIcsCalendar.getContentPane().add(calendarStart);
		
		//Listens for changes in JCalendarButton and updates corresponding JTextField
        calendarStart.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                if (evt.getNewValue() instanceof Date){
                	DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
                	String output = outputFormatter.format(evt.getNewValue());
                	txtStartDate.setForeground(Color.BLACK);
                	txtStartDate.setText(output);
                }
            }
        });
        
        JCalendarButton calendarEnd = new JCalendarButton();
		calendarEnd.setBounds(152, 38, 29, 29);
		frmIcsCalendar.getContentPane().add(calendarEnd);
		
		//Listens for changes in JCalendarButton and updates corresponding JTextField
		calendarEnd.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
			public void propertyChange(java.beans.PropertyChangeEvent evt) {
                if (evt.getNewValue() instanceof Date){
                	DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
                	String output = outputFormatter.format(evt.getNewValue());
                	txtEndDate.setForeground(Color.BLACK);
                	txtEndDate.setText(output);
                }
            }
        });
		
		txtStartDate = new JTextField();
		txtStartDate.setForeground(Color.LIGHT_GRAY);
		txtStartDate.setText("Start Date");
		txtStartDate.setBounds(6, 5, 134, 28);
		frmIcsCalendar.getContentPane().add(txtStartDate);
		txtStartDate.setColumns(10);
		
		txtStartDate.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtStartDate.getText().equals("Start Date")) {
	                txtStartDate.setForeground(Color.BLACK);
	            	txtStartDate.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtStartDate.getText().equals("")) {
	        		txtStartDate.setText("Start Date");
	        		txtStartDate.setForeground(Color.LIGHT_GRAY);
	        	}
	        }
	    });
		
		txtStartDate.getDocument().addDocumentListener(new DocumentListener() {
			  public void changedUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void removeUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void insertUpdate(DocumentEvent e) {
			    warn();
			  }

			  public void warn() {
				  if (!isValidDate(txtStartDate.getText())) {
					  txtStartDate.setForeground(Color.RED);
					  globalInvalidator = true;
				  } else {
					  txtStartDate.setForeground(Color.BLACK);
					  globalInvalidator = false;
				  }
			  }
		});
			
		txtEndDate = new JTextField();
		txtEndDate.setForeground(Color.LIGHT_GRAY);
		txtEndDate.setText("End Date");
		txtEndDate.setBounds(6, 39, 134, 28);
		frmIcsCalendar.getContentPane().add(txtEndDate);
		txtEndDate.setColumns(10);

		txtEndDate.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtEndDate.getText().equals("End Date")) {
	                txtEndDate.setForeground(Color.BLACK);
	                txtEndDate.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtEndDate.getText().equals("")) {
	        		txtEndDate.setText("End Date");
	        		txtEndDate.setForeground(Color.LIGHT_GRAY);
	        	}
	        }
	    });
		
		txtEndDate.getDocument().addDocumentListener(new DocumentListener() {
			  public void changedUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void removeUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void insertUpdate(DocumentEvent e) {
			    warn();
			  }

			  public void warn() {
				  if (!isValidDate(txtEndDate.getText())) {
					  txtEndDate.setForeground(Color.RED);
					  globalInvalidator = true;
				  } else {
					  txtEndDate.setForeground(Color.BLACK);
					  globalInvalidator = false;
				  }
			  }
		});
		
		txtStartHour = new JTextField();
		txtStartHour.setForeground(Color.LIGHT_GRAY);
		txtStartHour.setText("HH");
		txtStartHour.setBounds(193, 5, 34, 28);
		frmIcsCalendar.getContentPane().add(txtStartHour);
		txtStartHour.setColumns(10);

		txtStartHour.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtStartHour.getText().equals("HH")) {
	            	txtStartHour.setForeground(Color.BLACK);
	            	txtStartHour.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtStartHour.getText().equals("")) {
	        		txtStartHour.setText("HH");
	        		txtStartHour.setForeground(Color.LIGHT_GRAY);
	        	}
	        }
	    });
		
		txtStartHour.getDocument().addDocumentListener(new DocumentListener() {
			  public void changedUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void removeUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void insertUpdate(DocumentEvent e) {
			    warn();
			  }

			  public void warn() {
			     if (!isValidHour(txtStartHour.getText())){
			    	 txtStartHour.setForeground(Color.RED);
			    	 globalInvalidator = true;
			     } else {
			    	 txtStartHour.setForeground(Color.BLACK);
			    	 globalInvalidator = false;
			     }
			  }
		});
		
		txtEndHour = new JTextField();
		txtEndHour.setForeground(Color.LIGHT_GRAY);
		txtEndHour.setText("HH");
		txtEndHour.setColumns(10);
		txtEndHour.setBounds(193, 38, 34, 28);
		frmIcsCalendar.getContentPane().add(txtEndHour);

		txtEndHour.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtEndHour.getText().equals("HH")) {
	            	txtEndHour.setForeground(Color.BLACK);
	            	txtEndHour.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtEndHour.getText().equals("")) {
	        		txtEndHour.setText("HH");
	        		txtEndHour.setForeground(Color.LIGHT_GRAY);
	        	}
	        }
	    });
		
		txtEndHour.getDocument().addDocumentListener(new DocumentListener() {
			  public void changedUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void removeUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void insertUpdate(DocumentEvent e) {
			    warn();
			  }

			  public void warn() {
			     if (!isValidHour(txtStartHour.getText())){
			    	 txtEndHour.setForeground(Color.RED);
			    	 globalInvalidator = true;
			     } else {
			    	 txtEndHour.setForeground(Color.BLACK);
			    	 globalInvalidator = false;
			     }
			  }
		});
		
		JLabel label = new JLabel(":");
		label.setBounds(228, 9, 4, 16);
		frmIcsCalendar.getContentPane().add(label);
		
		JLabel label_1 = new JLabel(":");
		label_1.setBounds(228, 42, 4, 16);
		frmIcsCalendar.getContentPane().add(label_1);
		
		txtStartMin = new JTextField();
		txtStartMin.setForeground(Color.LIGHT_GRAY);
		txtStartMin.setText("MM");
		txtStartMin.setColumns(10);
		txtStartMin.setBounds(233, 5, 34, 28);
		frmIcsCalendar.getContentPane().add(txtStartMin);

		txtStartMin.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtStartMin.getText().equals("MM")) {
	            	txtStartMin.setForeground(Color.BLACK);
	            	txtStartMin.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtStartMin.getText().equals("")) {
	        		txtStartMin.setText("MM");
	        		txtStartMin.setForeground(Color.LIGHT_GRAY);
	        	}
	        }
	    });
		
		txtStartMin.getDocument().addDocumentListener(new DocumentListener() {
			  public void changedUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void removeUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void insertUpdate(DocumentEvent e) {
			    warn();
			  }

			  public void warn() {
			     if (!isValidMin(txtStartMin.getText())){
			    	 txtStartMin.setForeground(Color.RED);
			    	 globalInvalidator = true;
			     } else {
			    	 txtStartMin.setForeground(Color.BLACK);
			    	 globalInvalidator = false;
			     }
			  }
		});
		
		txtEndMin = new JTextField();
		txtEndMin.setForeground(Color.LIGHT_GRAY);
		txtEndMin.setText("MM");
		txtEndMin.setColumns(10);
		txtEndMin.setBounds(233, 38, 34, 28);
		frmIcsCalendar.getContentPane().add(txtEndMin);

		txtEndMin.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtEndMin.getText().equals("MM")) {
	            	txtEndMin.setForeground(Color.BLACK);
	            	txtEndMin.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtEndMin.getText().equals("")) {
	        		txtEndMin.setText("MM");
	        		txtEndMin.setForeground(Color.LIGHT_GRAY);
	        		
	        	}
	        }
	    });
		
		txtEndMin.getDocument().addDocumentListener(new DocumentListener() {
			  public void changedUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void removeUpdate(DocumentEvent e) {
			    warn();
			  }
			  public void insertUpdate(DocumentEvent e) {
			    warn();
			  }

			  public void warn() {
			     if (!isValidMin(txtEndMin.getText())){
			    	 txtEndMin.setForeground(Color.RED);
			    	 globalInvalidator = true;
			     } else {
			    	 txtEndMin.setForeground(Color.BLACK);
			    	 globalInvalidator = false;
			     }
			  }
		});
		
		final DefaultComboBoxModel model = new DefaultComboBoxModel (amPmList); 
		JComboBox comboStartAmPm = new JComboBox(model);
		comboStartAmPm.setBounds(279, 6, 70, 27);
		frmIcsCalendar.getContentPane().add(comboStartAmPm);
		
		final DefaultComboBoxModel model1 = new DefaultComboBoxModel (amPmList);
		JComboBox comboEndAmPm = new JComboBox(model1);
		comboEndAmPm.setBounds(279, 39, 70, 27);
		frmIcsCalendar.getContentPane().add(comboEndAmPm);
		
		
		/*
		 * TODO
		 * Implement comboTimeZone with Strings or TimeZone objects?
		 */
		final DefaultComboBoxModel model_1 = new DefaultComboBoxModel (tzList);
		JComboBox comboTimeZone = new JComboBox(model_1);
		comboTimeZone.setBounds(124, 73, 225, 27);
		// Set the default time zone to be the user's current time zone.
		for (int i = 0; i < tzList.length; i++) {
			if (tzList[i].hasSameRules(TimeZone.getDefault())) {
				comboTimeZone.setSelectedIndex(i);
				break;
			}
		}
		frmIcsCalendar.getContentPane().add(comboTimeZone);
		
		JLabel lblSelectTimeZone = new JLabel("Select Time Zone");
		lblSelectTimeZone.setHorizontalAlignment(SwingConstants.LEFT);
		lblSelectTimeZone.setBounds(6, 77, 134, 16);
		frmIcsCalendar.getContentPane().add(lblSelectTimeZone);
		
		
		txtName = new JTextField();
		txtName.setForeground(Color.LIGHT_GRAY);
		txtName.setText("Name");
		txtName.setBounds(102, 105, 247, 28);
		frmIcsCalendar.getContentPane().add(txtName);
		txtName.setColumns(10);

		txtName.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtName.getText().equals("Name")) {
	            	txtName.setForeground(Color.BLACK);
	            	txtName.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtName.getText().equals("")) {
	        		txtName.setForeground(Color.LIGHT_GRAY);
	        		txtName.setText("Name");
	        	}
	        }
	    });
		
		JLabel lblEventName = new JLabel("Event Name");
		lblEventName.setHorizontalAlignment(SwingConstants.CENTER);
		lblEventName.setBounds(6, 111, 84, 16);
		frmIcsCalendar.getContentPane().add(lblEventName);
		
		txtLocation = new JTextField();
		txtLocation.setForeground(Color.LIGHT_GRAY);
		txtLocation.setText("Location");
		txtLocation.setColumns(10);
		txtLocation.setBounds(102, 133, 247, 28);
		frmIcsCalendar.getContentPane().add(txtLocation);

		txtLocation.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtLocation.getText().equals("Location")) {
	            	txtLocation.setForeground(Color.BLACK);
	            	txtLocation.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtLocation.getText().equals("")) {
	        		txtLocation.setForeground(Color.LIGHT_GRAY);
	        		txtLocation.setText("Location");
	        	}
	        }
	    });
		
		JLabel lblLocation = new JLabel("Location");
		lblLocation.setHorizontalAlignment(SwingConstants.CENTER);
		lblLocation.setBounds(6, 139, 84, 16);
		frmIcsCalendar.getContentPane().add(lblLocation);
		
		txtResources = new JTextField();
		txtResources.setForeground(Color.LIGHT_GRAY);
		txtResources.setText("Resources");
		txtResources.setColumns(10);
		txtResources.setBounds(102, 161, 247, 28);
		frmIcsCalendar.getContentPane().add(txtResources);

		txtResources.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtResources.getText().equals("Resources")) {
	            	txtResources.setForeground(Color.BLACK);
	            	txtResources.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtResources.getText().equals("")) {
	        		txtResources.setForeground(Color.LIGHT_GRAY);
	        		txtResources.setText("Resources");
	        	}
	        }
	    });
		
		JLabel lblResources = new JLabel("Resources");
		lblResources.setHorizontalAlignment(SwingConstants.CENTER);
		lblResources.setBounds(6, 167, 84, 16);
		frmIcsCalendar.getContentPane().add(lblResources);
		
		final JEditorPane dtrpnDescription = new JEditorPane();
		dtrpnDescription.setForeground(Color.LIGHT_GRAY);
		dtrpnDescription.setText("Description...");
		dtrpnDescription.setBounds(8, 230, 338, 145);
		frmIcsCalendar.getContentPane().add(dtrpnDescription);
		
		dtrpnDescription.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (dtrpnDescription.getText().equals("Description...")) {
	            	dtrpnDescription.setForeground(Color.BLACK);
	            	dtrpnDescription.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (dtrpnDescription.getText().equals("")) {
	        		dtrpnDescription.setForeground(Color.LIGHT_GRAY);
	        		dtrpnDescription.setText("Description");
	        	}
	        }
	    });
		
		JLabel lblRecurrence = new JLabel("Repeat Every:");
		lblRecurrence.setHorizontalAlignment(SwingConstants.LEFT);
		lblRecurrence.setBounds(6, 420, 86, 16);
		frmIcsCalendar.getContentPane().add(lblRecurrence);
		
		JComboBox comboEventRepeats = new JComboBox();
		comboEventRepeats.setBounds(152, 383, 196, 27);
		//frame.getContentPane().add(comboEventRepeats);
		
		
		/**********************************************
		 *  section of recurrent 
		 *  
		 *  Start
		 *  
		 *  
		 ***********************************************/
		buttonGroup1= new ButtonGroup();
		
		chckbxNewCheckBox = new JRadioButton("Day");
		chckbxNewCheckBox.setBounds(102,414,60, 27);
		frmIcsCalendar.getContentPane().add(chckbxNewCheckBox);
		
		chckbxNewCheckBox_1 = new JRadioButton("Week");
		chckbxNewCheckBox_1.setBounds(174, 414, 75, 27);
		frmIcsCalendar.getContentPane().add(chckbxNewCheckBox_1);
		
		chckbxNewCheckBox_2 = new JRadioButton("Month");
		chckbxNewCheckBox_2.setBounds(250,414, 75, 27);
		frmIcsCalendar.getContentPane().add(chckbxNewCheckBox_2);
		
		chckbxNewCheckBox_3 = new JRadioButton("Year");
		chckbxNewCheckBox_3.setBounds(102, 435, 60, 27);
		frmIcsCalendar.getContentPane().add(chckbxNewCheckBox_3);
		
		
		customWindow = new Custom();
		chckbxNewCheckBox_4 = new JRadioButton("Other");
		chckbxNewCheckBox_4.setBounds(174, 435, 70, 27);
		chckbxNewCheckBox_4.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(ActionEvent e) {
				customWindow.setVisible(true);
				
				
			}
        });
		frmIcsCalendar.getContentPane().add(chckbxNewCheckBox_4);
		
		rdbtnNoRepeat = new JRadioButton("No Repeat", true);
		rdbtnNoRepeat.setBounds(250, 437, 99, 23);
		frmIcsCalendar.getContentPane().add(rdbtnNoRepeat);
		
		/**********************************************
		 *   end of  
		 *  
		 *  the section of recurrent
		 *  
		 *  
		 ***********************************************/
		
		
		
		btnResetFields = new JButton("Reset Fields");
		btnResetFields.setBounds(28, 469, 134, 29);
		frmIcsCalendar.getContentPane().add(btnResetFields);
		btnResetFields.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
		        txtStartDate.setText("Start Date");
		        txtStartDate.setForeground(Color.LIGHT_GRAY);
		        txtEndDate.setText("End Date");
		        txtEndDate.setForeground(Color.LIGHT_GRAY);
		        txtStartHour.setText("HH");
		        txtStartHour.setForeground(Color.LIGHT_GRAY);
		        txtEndHour.setText("HH");
		        txtEndHour.setForeground(Color.LIGHT_GRAY);
		        txtStartMin.setText("MM");
		        txtStartMin.setForeground(Color.LIGHT_GRAY);
		        txtEndMin.setText("MM");
		        txtEndMin.setForeground(Color.LIGHT_GRAY);
		        txtName.setText("Name");
		        txtName.setForeground(Color.LIGHT_GRAY);
		        txtLocation.setText("Location");
		        txtLocation.setForeground(Color.LIGHT_GRAY);
		        txtResources.setText("Resources");
		        txtResources.setForeground(Color.LIGHT_GRAY);
		        txtAttachment.setText("URI of Attachment");
		        txtAttachment.setForeground(Color.LIGHT_GRAY);
		        dtrpnDescription.setText("Description...");
		        dtrpnDescription.setForeground(Color.LIGHT_GRAY);
		        buttonGroup1.clearSelection();
		        rdbtnNoRepeat.setSelected(true);
		    }
		});
		
		
		
		
		btnCreateEvent = new JButton("Create Event");
		btnCreateEvent.setBounds(191, 469, 134, 29);
		frmIcsCalendar.getContentPane().add(btnCreateEvent);
		
		txtAttachment = new JTextField();
		txtAttachment.setForeground(Color.LIGHT_GRAY);
		txtAttachment.setText("URI of Attachment");
		txtAttachment.setBounds(102, 190, 247, 28);
		frmIcsCalendar.getContentPane().add(txtAttachment);
		txtAttachment.setColumns(10);
		
		txtAttachment.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtAttachment.getText().equals("URI of Attachment")) {
	            	txtAttachment.setForeground(Color.BLACK);
	            	txtAttachment.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtAttachment.getText().equals("")) {
	        		txtAttachment.setForeground(Color.LIGHT_GRAY);
	        		txtAttachment.setText("URI of Attachment");
	        	}
	        }
	    });
		
		JLabel lblAttachment = new JLabel("Attachment");
		lblAttachment.setHorizontalAlignment(SwingConstants.CENTER);
		lblAttachment.setBounds(6, 195, 84, 16);
		frmIcsCalendar.getContentPane().add(lblAttachment);
		
		//Reminder
		lblReminder = new JLabel("Reminder:");
		lblReminder.setHorizontalAlignment(SwingConstants.LEFT);
		lblReminder.setBounds(6, 386, 86, 16);
		frmIcsCalendar.getContentPane().add(lblReminder);
		
		txtReminder = new JTextField();
		txtReminder.setText("###");
		txtReminder.setForeground(Color.LIGHT_GRAY);
		txtReminder.setColumns(10);
		txtReminder.setBounds(102, 379, 60, 29);
		frmIcsCalendar.getContentPane().add(txtReminder);
		
		txtReminder.addFocusListener(new FocusListener() {

	        @Override
	        public void focusGained(FocusEvent e) {
	            if (txtReminder.getText().equals("###")) {
	            	txtReminder.setForeground(Color.BLACK);
	            	txtReminder.setText("");
	            }
	        }

	        @Override
	        public void focusLost(FocusEvent e) {
	        	if (txtReminder.getText().equals("")) {
	        		txtReminder.setForeground(Color.LIGHT_GRAY);
	        		txtReminder.setText("###");
	        	}
	        }
	    });
		
		comboReminder = new JComboBox();
		comboReminder.setModel(new DefaultComboBoxModel(new String[] {"Minute", "Hour", "Day", "Week"}));
		comboReminder.setBounds(174, 379, 93, 28);
		frmIcsCalendar.getContentPane().add(comboReminder);
		
		btnCreateEvent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (txtStartDate.getText().equals("Start Date") || txtEndDate.getText().equals("End Date") || txtStartHour.getText().equals("MM") || 
						txtEndHour.getText().equals("MM") || txtStartMin.getText().equals("MM") || txtEndMin.getText().equals("MM") || txtName.getText().equals("")) {
					JOptionPane.showMessageDialog(frmIcsCalendar,
							"You must supply a start time and date, end time and date, and a name for this event.",
						    "Invalid Event",
						    JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				if (globalInvalidator) {
					JOptionPane.showMessageDialog(frmIcsCalendar,
							"One or more fields contain invalid information. Please fix any fields colored red.",
						    "Invalid Event",
						    JOptionPane.ERROR_MESSAGE);
					return;
				} else if (!isValidOrder(txtStartDate.getText(), txtEndDate.getText(), txtStartHour.getText(), txtEndHour.getText()
						, txtStartMin.getText(), txtEndMin.getText(), model.getSelectedItem(), model1.getSelectedItem())) {
					JOptionPane.showMessageDialog(frmIcsCalendar,
							"Start and end dates/times are not chronologically correct.",
						    "Invalid Event",
						    JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				
				
				
				/*************************************
				 *   Beginning of setting up recurrent 
				 *************************************/
				String recurrentString ="";
				
				if(chckbxNewCheckBox.isSelected()){
					recurrentString = "RRULE:FREQ=DAILY";
				}else if(chckbxNewCheckBox_1.isSelected()){
					recurrentString = "RRULE:FREQ=WEEKLY";
				}else if(chckbxNewCheckBox_2.isSelected()){
					recurrentString = "RRULE:FREQ=MONTHLY";
				}else if(chckbxNewCheckBox_3.isSelected()){
					recurrentString = "RRULE:FREQ=YEARLY";
				}else if(chckbxNewCheckBox_4.isSelected()){
					recurrentString = customWindow.getRecurrentString();
				}else if(rdbtnNoRepeat.isSelected()){
					recurrentString ="";
				}/*************************************
				 *   End of setting up recurrent 
				 *************************************/
				
				
				
				
				
				eventWriter.setVersion("2.0"); //arbitrarily set to 2.0
				eventWriter.setClassification("VEVENT"); //app only deals with events for now, could implement options for other types
				eventWriter.setComment(dtrpnDescription.getText());
				eventWriter.setLocation(txtLocation.getText());
				eventWriter.setResources(txtResources.getText());
				eventWriter.setDTSTART(parseDate(txtStartDate, txtStartHour, txtStartMin, model));
				eventWriter.setDTEND(parseDate(txtEndDate, txtEndHour, txtEndMin, model1));
				eventWriter.setTimeZone(((TimeZoneEntry) model_1.getSelectedItem()).getID());
				eventWriter.setRecurring(recurrentString);
				eventWriter.setAttachment(txtAttachment.getText());
				eventWriter.setReminder(txtReminder.getText(), comboReminder.getSelectedItem().toString());
				eventWriter.print();
			}
		});
		
		groupUpButtons();
	}
	
	public void groupUpButtons(){
        buttonGroup1.add(chckbxNewCheckBox);
        buttonGroup1.add(chckbxNewCheckBox_1);
        buttonGroup1.add(chckbxNewCheckBox_2);
        buttonGroup1.add(chckbxNewCheckBox_3);
        buttonGroup1.add(chckbxNewCheckBox_4);
        buttonGroup1.add(rdbtnNoRepeat);
    }
	
	/**
	 * Parses input from date, hour, and minute text fields into DATE-TIME format of .ics files
	 * 
	 * @param date: Text field containing date
	 * @param hour: Text field containing hour
	 * @param min:  Text field containing minute
	 */
	public String parseDate(JTextField date, JTextField hour, JTextField min, DefaultComboBoxModel amPm) {
		Date systemDate = new Date();
		int hourSubstring = (amPm.getSelectedItem().equals("PM")) ? 12 + Integer.parseInt(hour.getText()) : Integer.parseInt(hour.getText());
		String formattedDate = date.getText().substring(6)
				+ date.getText().substring(0, 2)
				+ date.getText().substring(3, 5)
				+ "T"
				+ hourSubstring
				+ min.getText()
				+"00";
		return formattedDate;
	}
	
	/**
	 * Validates date fields
	 * 
	 * @param inputDate: String from date JTextField
	 * @return
	 * 		True: inputDate is a valid date
	 * 		False: inputDate is an invalid date
	 */
	public boolean isValidDate(String inputDate) {
		Date date;
		currentDate = new Date();
		sdf.setLenient(false);
		
		try {
			date = sdf.parse(inputDate);
		} catch (ParseException e) {
			return false;
		}
		
		if (date.before(currentDate) && !sdf.format(date).equals(sdf.format(currentDate))) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * Validates hour fields
	 * 
	 * @param inputHour: String from hour JTextfield
	 * @return
	 * 		True: inputHour is a valid hour
	 * 		False: inputHour is an invalid hour
	 */
	public boolean isValidHour(String inputHour) {
		currentDate = new Date();
		int hours;
		
		if (inputHour.length() < 2) {
			return false;
		}
		
		try {
			hours = Integer.parseInt(inputHour);
		} catch (NumberFormatException e) {
			return false;
		}
		
		if (hours > 12){
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * Validates minute fields
	 * 
	 * @param inputMin: String from minute JTextField
	 * @return
	 * 		True: inputMin is a valid minute
	 * 		False: inputMin is an invalid minute
	 */
	public boolean isValidMin(String inputMin) {
		currentDate = new Date();
		int mins;
		
		if (inputMin.length() < 2) {
			return false;
		}
		try {
			mins = Integer.parseInt(inputMin);
		} catch (NumberFormatException e) {
			return false;
		}
		
		if (mins > 60){
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * Validates order of dates and times
	 * 
	 * @param startDateString: String from starting date JTextField
	 * @param endDateString: String from ending date JTextField
	 * @param startHourString: String from starting hour JTextField
	 * @param endHourString: String from ending hour JTextField
	 * @param startMinString: String from starting minute JTextField
	 * @param endMinString: String from ending minute JTextField
	 * @param startAmPmCombo: Selection from starting AM/PM combo box
	 * @param endAmPmCombo: Selection from ending AM/PM combo box
	 * @return
	 * 		True: Dates and times are in a valid order
	 * 		False: Dates and times are in an invalid order
	 */
	public boolean isValidOrder(String startDateString, String endDateString, String startHourString, String endHourString, 
			String startMinString, String endMinString, Object startAmPmCombo, Object endAmPmCombo) {
		Date startDate = null;
		Date endDate = null;
		int startHour = 0;
		int endHour = 0;
		int startMin = 0;
		int endMin = 0;
		boolean isValid = false;
		
		try {
			startDate = sdf.parse(startDateString);
			endDate = sdf.parse(endDateString);
		} catch (ParseException e) {
			isValid = false;
		}
		try {
			startHour = Integer.parseInt(startHourString);
			endHour = Integer.parseInt(endHourString);
			startMin = Integer.parseInt(startMinString);
			endMin = Integer.parseInt(endMinString);
		} catch (NumberFormatException e) {
			isValid = false;
		}
		
		if (sdf.format(startDate).equals(sdf.format(endDate))) {
			if (startAmPmCombo.equals(endAmPmCombo)) {
				
				if (startHour == endHour) {
					
					if (startMin >= endMin) {
						isValid = false;
					} else {
						isValid = true;
					}
					
				} else if (startHour > endHour) {
					isValid = false;
				} else {
					return true;
				}
				
			} else if (startAmPmCombo.equals("PM")) {
				isValid = false;
			} else {
				isValid = true;
			}
			
		} else if (startDate.after(endDate)) {
			isValid = false;
		} else {
			isValid = true;
		}
		
		return isValid;
	}
	

	public void setRecurrentString(String s){
		customWindowString = s;
		if(s.equals("")){
			rdbtnNoRepeat.setSelected(true);
		}
	}
}